<?php

namespace app\controller\admin\sky;

use app\model\api\SkyActivity;
use laytp\controller\Backend;
use laytp\library\CommonFun;
use think\facade\Db;
use think\response\Json;

/**
 * Class Team
 * @package app\controller\admin\sky
 */
class Activity extends Backend
{
    /**
     *
     * @var Activity
     */
    protected $model;
    protected $hasSoftDel=0;//是否拥有软删除功能

    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法

    public $orderRule = ['sort' => 'desc', 'id' => 'asc'];

    public function _initialize()
    {
        $this->model = new SkyActivity();
    }


    //添加
    public function add()
    {
        try {
            $post     = $this->request->post();
            if(empty($post['time'])){
                throw new \Exception('请选择日期');
            }
            $all_date = explode(' - ',$post['time']);

            $saveData = [
                'start_time' => strtotime(trim($all_date[0])),
                'end_time'   => strtotime(trim($all_date[1])),
                'icon'       => $post['icon'],
                'tag'        => $post['tag'],
                'now_tag'       => $post['now_tag'],
                'activity_name' => $post['activity_name'],
                'activity_desc' => $post['activity_desc'],
                'target'        => !empty($post['target'])?$post['target']:0,
                'target_season' => !empty($post['target_season'])?$post['target_season']:'',
                'activity_img'  => json_encode(explode(',',$post['image']),JSON_UNESCAPED_UNICODE),
            ];

            $saveAll = $this->model->save($saveData);
            if (!$saveAll) throw new \Exception('保存失败');

            $this->clearCache();
            return $this->success('操作成功');
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }


    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit()
    {
        $id = $this->request->param('id');
        $post = CommonFun::filterPostData($this->request->post());
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');

        if(empty($post['time'])){
            throw new \Exception('请选择日期');
        }
        $all_date = explode(' - ',$post['time']);

        $row->start_time    = strtotime(trim($all_date[0]));
        $row->end_time      = strtotime(trim($all_date[1]));
        $row->tag           = $post['tag'];
        $row->now_tag       = $post['now_tag'];
        $row->icon          = $post['icon'];
        $row->activity_name = $post['activity_name'];
        $row->activity_desc = $post['activity_desc'];
        $row->target        = !empty($post['target'])?$post['target']:0;
        $row->target_season = !empty($post['target_season'])?$post['target_season']:'';
        $row->activity_img  = json_encode(explode(',',$post['image']),JSON_UNESCAPED_UNICODE);
        try {
            $save = $row->save();
            if ($save === false) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    public function info(){
        $id = $this->request->param('id');

        $activity = $this->model->where('id',$id)->find()->toArray();
        $activity['activity_img'] = json_decode($activity['activity_img'],true);
        $activity['time'] = date('Y-m-d H:i:s',$activity['start_time']).' - '.date('Y-m-d H:i:s',$activity['end_time']);

        return $this->success('获取成功', $activity);
    }
}